package com.codeyang.config;

import com.codeyang.constant.QueueConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述:  订单服务的 MQ 队列相关配置
 *
 * @author CodeYang_Site
 * @version 2021/6/1 19:24
 */
@Configuration
public class OrderQueueConfig {

    //------ 配置 修改prod - es 监听队列 三件套-----
    // queue ex binding

    @Bean
    public Queue prodChangeQueue() {
        return new Queue(QueueConstant.PROD_CHANGE_QUEUE);
    }

    @Bean
    public DirectExchange prodChangeEx() {
        return new DirectExchange(QueueConstant.PROD_CHANGE_EX);
    }

    @Bean
    public Binding prodChangeBind() {
        return BindingBuilder.bind(prodChangeQueue()).to(prodChangeEx()).with(QueueConstant.PROD_CHANGE_KEY);
    }

    //----- 配置 prod 死信队列 四件套
    // 1 接收消息的延迟队列  配置死信队列相关信息  2 死信队列 监听它就可以3 交换机 4 绑定关系
    @Bean
    public Queue orderMsQueue() {
        Map<String, Object> map = new HashMap<>(6);
        //1.消息从延迟队列死后走那个交换机
        map.put("x-dead-letter-exchange", QueueConstant.ORDER_DEAD_EX);
        //2.消息死亡后走那个路由key
        map.put("x-dead-letter-routing-key", QueueConstant.ORDER_DEAD_KEY);
        //3.消息的死亡时间
        //一分钟
        map.put("x-message-ttl", 120 * 1000);
        return new Queue(QueueConstant.ORDER_MS_QUEUE, true, false, false, map);
    }

    @Bean
    public Queue orderDeadQueue() {
        return new Queue(QueueConstant.ORDER_DEAD_QUEUE);
    }

    @Bean
    public DirectExchange orderDeadEx() {
        return new DirectExchange(QueueConstant.ORDER_DEAD_EX);
    }

    @Bean
    public Binding orderDeadBing() {
        return BindingBuilder.bind(orderDeadQueue()).to(orderDeadEx()).with(QueueConstant.ORDER_DEAD_KEY);
    }

}
